package com.gexin.service.conpon;

import com.gexin.common.model.ApiResponse;
import com.gexin.common.utils.StringUtil;
import com.gexin.model.activity.Coupon;
import com.gexin.model.product.Products;
import com.gexin.model.user.UserCoupon;
import com.gexin.model.weixin.WeixinUser;

import java.util.ArrayList;
import java.util.List;

public class UserCouponService {
	public static UserCouponService dao = new UserCouponService();

	/**
	 * 获取订单中用户可用优惠券
	 * @param productIds 商品id
	 * @param authUser
	 */
	public ApiResponse findAvailableCouponList(String productIds , WeixinUser authUser) {
		if(StringUtil.isBlank(productIds)){
			return new ApiResponse(false,"商品id不能为null!");
		}
		List<Products> products = Products.dao.find("select * from t_products where id in (" + productIds + ")");
		//先查询所有未使用的优惠券
		List<UserCoupon> coupons = UserCoupon.dao.findAllStatusAndUserId(UserCoupon.STATE_WEISHIYONG , authUser.getId());
		List<UserCoupon> returnList = new ArrayList<>();
		for (UserCoupon coupon : coupons) {
			if(Coupon.USETYPE_ALL.equals(coupon.getInt("used"))){
				returnList.add(coupon);
			}else if(Coupon.USETYPE_PRODUCT.equals(coupon.getInt("used"))){
				for(Products product : products){
					System.err.println(product.getId());
					System.err.println(coupon.getInt("product_id"));
					if(product.getId().equals(coupon.getInt("product_id")) || product.getId()== coupon.getInt("product_id")){
						returnList.add(coupon);
						break;
					}
				}
			}else if(Coupon.USETYPE_NEWUSER.equals(coupon.getInt("used"))){

			}else if(Coupon.USETYPE_PRODUCTTYPE.equals(coupon.getInt("used"))){
				for(Products product : products){
					if(product.getTypeId().equals(coupon.getInt("product_type_id"))){
						returnList.add(coupon);
						break;
					}
				}
			}
		}
		return new ApiResponse(true,"success",returnList);
	}
}
